package com.heatup.apt.common.util;
import com.heatup.apt.common.dao.TokenMapper;
import com.heatup.apt.common.dao.redis.TokenRedisDao;
import com.heatup.apt.model.weixin.Template;
import com.heatup.apt.model.weixin.Token;

public class SendTemplateThread implements Runnable {
	
	private TokenMapper tokenMapper;
	
	private TokenRedisDao tokenRedisDao;

	public TokenRedisDao getTokenRedisDao() {
		return tokenRedisDao;
	}

	public void setTokenRedisDao(TokenRedisDao tokenRedisDao) {
		this.tokenRedisDao = tokenRedisDao;
	}

	private Template template;
	
	
	public TokenMapper getTokenMapper() {
		return tokenMapper;
	}

	public void setTokenMapper(TokenMapper tokenMapper) {
		this.tokenMapper = tokenMapper;
	}
	
	public SendTemplateThread(Template template ,TokenMapper tokenMapper,TokenRedisDao tokenRedisDao){
		this.tokenMapper=tokenMapper;
		this.template=template;
		this.tokenRedisDao=tokenRedisDao;
	};

	@Override
	public void run() {
		int i=0;
		 while(true){
			 	//Token token = tokenMapper.selectTokenByKey(TokenConstants.ACCESS_TOKEN_KEY);
			 	Token token = tokenRedisDao.queryTokenByRedis();
	        	if(i>=3 ||TemplateMsgUtil.sendTemplateMsg(template, token.getToken())){
	        		break;
	        	}
	        	i++;
	        	try {
					Thread.sleep(1000*60*5);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
	        }
	}

}
